package com.base.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.base.common.DBAccess;
import com.base.model.EntityData;
import com.base.model.Knowledge;

public class KnowledgeDAO {
	
private DBAccess dbAccess = new DBAccess();

	
	public DBAccess getDbAccess() {
		return dbAccess;
	}


	public void setDbAccess(DBAccess dbAccess) {
		this.dbAccess = dbAccess;
	}
	
	public Knowledge load(String id){
		Knowledge info = new  Knowledge();
		List<Knowledge> resultList = new ArrayList<Knowledge>();
		try {
			if(dbAccess.createConn()){
				String sql = "select * from tb_knowledge where KNOWLEDGE_ID = '" + id + "'";
				dbAccess.query(sql);
				if(dbAccess.next()){
					ResultSet rs = dbAccess.getRs();
					Knowledge entity = new  Knowledge();
					entity.setKnowledgeId(rs.getString("KNOWLEDGE_ID"));
					entity.setKnowledgeSubject(rs.getString("KNOWLEDGE_SUBJECT"));
					entity.setKnowledgeContent(rs.getString("KNOWLEDGE_CONTENT"));
//					entity.setCreateUserId(rs.getString("CREATE_USER_ID"));
//					entity.setCreateTime(rs.getDate("CREATE_TIME"));
//					entity.setUpdateUserId(rs.getString("UPDATE_USER_ID"));
//					entity.setUpdateTime(rs.getDate("UPDATE_TIME"));
//					entity.setDeleteFlag(rs.getString("DELETE_FLAG"));
					resultList.add(entity);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		if(resultList.size() > 0){
			info = resultList.get(0);
		}
		return info;
	}
	
	public boolean insert(Knowledge info){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "INSERT INTO tb_knowledge "+
						"(KNOWLEDGE_ID, KNOWLEDGE_SUBJECT, KNOWLEDGE_CONTENT) " +
						"VALUES ('" + info.getKnowledgeId() + "', '" + info.getKnowledgeSubject() + "', '" + info.getKnowledgeContent() + "')";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
	
	public List<? extends EntityData> list(){
		List<Knowledge> results = new ArrayList<Knowledge>();
		try {
			if(dbAccess.createConn()){
				String sql = "select * from tb_knowledge ";
				dbAccess.query(sql);
				while(dbAccess.next()){
					ResultSet rs = dbAccess.getRs();
					Knowledge entity = new  Knowledge();
					entity.setKnowledgeId(rs.getString("KNOWLEDGE_ID"));
					entity.setKnowledgeSubject(rs.getString("KNOWLEDGE_SUBJECT"));
					entity.setKnowledgeContent(rs.getString("KNOWLEDGE_CONTENT"));
//					entity.setCreateUserId(rs.getString("CREATE_USER_ID"));
//					entity.setCreateTime(rs.getDate("CREATE_TIME"));
//					entity.setUpdateUserId(rs.getString("UPDATE_USER_ID"));
////					entity.setUpdateTime(rs.getDate("UPDATE_TIME"));
//					entity.setDeleteFlag(rs.getString("DELETE_FLAG"));
					results.add(entity);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return results;
	}
	
	public boolean delete(String id){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "delete from tb_knowledge where KNOWLEDGE_ID = '"+id +"'";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
	
	public boolean update(Knowledge info){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "update tb_knowledge "+
						"set KNOWLEDGE_SUBJECT = '" + info.getKnowledgeSubject() + "', KNOWLEDGE_CONTENT = '" + info.getKnowledgeContent() + "' " +
								"where KNOWLEDGE_ID ='" + info.getKnowledgeId() + "'";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
}
